package com.smart.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.smart.model.Admin;
import com.smart.service.AdminService;
import com.smart.util.MD5Util;

@Controller
public class AdminOperController {
	@RequestMapping(value="/pc/get_admin_list", method = RequestMethod.POST)
    public void getAdminList(HttpServletRequest req,HttpServletResponse resp)
    {
		  System.out.println("getAdminList method start");
		  HttpSession session=req.getSession();
		  Admin admin=(Admin)session.getAttribute("admin");
		  int communityId=admin.getCommunityId();
		  System.out.println("communityId:"+communityId);
		   //第几页
		   int pageIndex=Integer.parseInt(req.getParameter("page"));
		   int pageSize=10;
		   AdminService adminSer=new AdminService();
		   List<Admin> adminList=adminSer.getAdminList(pageIndex, pageSize,communityId);
		   int count=adminSer.getCount(communityId);
		   JSONObject jsonObject=new JSONObject();
		   jsonObject.put("count",count);
		   jsonObject.put("content",adminList);
		   System.out.println(jsonObject);
		   PrintWriter out=null;
		  try {
			out = resp.getWriter();
			out.print(jsonObject.toString());
		  } catch (IOException e) {
			e.printStackTrace();
		  }
		  finally
		  {
			  if(out!=null) 
				  out.close();
		  }
		   System.out.println("getAdminList method finish");
    }
	
	 //添加管理员
	@RequestMapping(value="/pc/add_admin", method = RequestMethod.POST)
	public void addAdmin(HttpServletRequest req,HttpServletResponse resp)
	{
		System.out.println("addAdmin method start.....");
		HttpSession session=req.getSession();
		Admin admin=(Admin)session.getAttribute("admin");
		int communityId=admin.getCommunityId();
		System.out.println("communityId:"+communityId);
		String adminName=req.getParameter("adminName");
		String password=req.getParameter("password");
		try 
		{
			//密码必须加密
			password=MD5Util.md5Encode(password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String telephoneNum=req.getParameter("telephoneNum");
		int type=Integer.parseInt(req.getParameter("type"));
		AdminService adminSer=new AdminService();
		int x=adminSer.addAdmin(password, type, telephoneNum, adminName,communityId);
		boolean success=false;
		PrintWriter out=null;
		try 
		{
			out=resp.getWriter();
			if(x>0)  success=true;
			out.print(success);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally{
			if(out!=null)   out.close();
		}
		System.out.println("addAdmin method finish.....");
	}
	
	
	//删除管理员
	@RequestMapping(value="/pc/delete_admin", method = RequestMethod.POST)
	public void deleteAdmin(HttpServletRequest req,HttpServletResponse resp)
	{
		System.out.println("deleteAdmin method start.....");
		int id=Integer.parseInt(req.getParameter("id"));
		AdminService adminSer=new AdminService();
		int x=adminSer.deleteAdmin(id);
		boolean success=false;
		PrintWriter out=null;
		try 
		{
			out=resp.getWriter();
			if(x>0)  success=true;
			out.print(success);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally{
			if(out!=null)   out.close();
		}
		System.out.println("deleteAdmin method finish.....");
	}
	
	//修改管理员信息
	@RequestMapping(value="/pc/update_admin", method = RequestMethod.POST)
	public void updateAdmin(HttpServletRequest req,HttpServletResponse resp)
	{
		System.out.println("updateAdmin method start....");
		boolean success=false;
		String adminName=req.getParameter("adminName");
		String telephoneNum=req.getParameter("telephoneNum");
		String password=req.getParameter("password");
		if(password!=null)
		{
			try {
				password=MD5Util.md5Encode(password);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		int type=Integer.parseInt(req.getParameter("type"));
		int id=Integer.parseInt(req.getParameter("id"));
		System.out.println("id:"+id);
		AdminService adminSer=new AdminService();
		Admin admin=new Admin();
		admin.setAdminName(adminName);
		admin.setId(id);
		admin.setPassword(password);
		admin.setTelephoneNum(telephoneNum);
		admin.setType(type);
		if(adminSer.updateAdmin(admin)>0) success=true;
		PrintWriter out=null;
		try {
			out=resp.getWriter();
			out.print(success);
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally
		{
			if(out!=null) out.close();
		}
		System.out.println("updateAdmin method finish....");
	}
	
	//搜索管理员信息，根据电话号码和管理员名字
	@RequestMapping(value="/pc/search_admin", method = RequestMethod.POST)
	public void searchAdmin(HttpServletRequest req,HttpServletResponse resp)
	{
		    System.out.println("hello searchAdmin method start....");
		    HttpSession session=req.getSession();
			Admin admin=(Admin)session.getAttribute("admin");
			int communityId=admin.getCommunityId();
			System.out.println("communityId:"+communityId);
			String search=req.getParameter("search");
			AdminService adminSer=new AdminService();
			List<Admin> adminList=null;
			int count;
			//如果搜索的内容框为空，那么就是默认搜索所有的内容，不过只返回第一页
			if(search==null||search.trim().equals(""))
			{
				 int pageIndex=1;
				 int pageSize=10;
				adminList=adminSer.getAdminList(pageIndex, pageSize,communityId);
				count=adminSer.getCount(communityId);
			}
			else
			{
			  System.out.println("search:"+search);
			 //由于不知道是电话号码还是管理员名字，所以都进行搜索
			 List<Admin> adminListName=adminSer.searchAdminByAdminName(search,communityId);
			 List<Admin> adminListTele=adminSer.searchAdminByTele(search,communityId);
			 adminList=new ArrayList<Admin>();
			 for(Admin adminTmp:adminListName)
				adminList.add(adminTmp);
			 for(Admin adminTmp:adminListTele)
				adminList.add(adminTmp);
			 count=adminList.size();
			}
			 JSONObject jsonObject=new JSONObject();
			 jsonObject.put("count",count);
		     jsonObject.put("content",adminList);
			 System.out.println(jsonObject);
			 PrintWriter out=null;
			 try {
				out = resp.getWriter();
				out.print(jsonObject.toString());
			 } catch (IOException e) {
				e.printStackTrace();
			}
			finally
			{
				if(out!=null) 	  out.close();
			}
			System.out.println("searchAdmin method finish....");
	}
	
	public static void main(String[] args)
	{
	}
}
